package com.itheima.reggie.controller;

import com.itheima.reggie.common.OssTemplate;
import com.itheima.reggie.common.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class FileController {

    @Autowired
    private OssTemplate ossTemplate;

    //文件上传
    @PostMapping("/common/upload")
    public ResultInfo uploadFile(MultipartFile file)throws IOException {
        if (file.getSize() > 0) {
            //1.将接收到的文件使用OssTemplate上传到阿里云
            String filePath = ossTemplate.upload(file.getOriginalFilename(), file.getInputStream());
            //2.上传之后访问地址返回给前端
            return ResultInfo.success(filePath);
        }
        return ResultInfo.error("上传失败");
    }
}
